package com.blue.http.filter;

import com.blue.http.message.HttpRequest;
import com.blue.http.message.HttpResponse;
import com.blue.http.util.HttpException;

import java.util.UUID;

/**
 * 产生Http会话过滤器
 *
 * @author Jin Zheng
 * @date 2018-10-29
 */
public class SessionCookieFilter implements Filter
{
	public static final String SESSION = "session";

	public SessionCookieFilter()
	{
	}

	@Override
	public boolean preHandle(HttpRequest request, HttpResponse response) throws HttpException
	{
		String session = null;
		if (request.getCookieMap() != null && !request.getCookieMap().isEmpty())
		{
			session = request.getCookie(SESSION);
		}
		if (session == null || session.isEmpty())
		{
			String value = UUID.randomUUID().toString();
			response.setCookie(SESSION, value);
		}

		return true;
	}

}
